﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiTrailCreator.Properties;
using System.IO;

namespace WikiTrailCreator.TrailImages
{
    public partial class FormTrailImages : Form
    {
        BindingList<TrailImage> trailImageListItems = new BindingList<TrailImage>();

        public FormTrailImages()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            GlobalImageGalleryContainer.Instance.Images = trailImageListItems.ToArray();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonAddImage_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = openFileDialog1.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string[] filePaths = openFileDialog1.FileNames;
                string[] fileNames = openFileDialog1.SafeFileNames;

                TrailImage newTrailImagesListItem = null;
                for (int i = 0; i < fileNames.Length; i++)
                {
                    string fileName = fileNames[i];
                    string filePath = filePaths[i];
                    long fileSize = (new FileInfo(filePath)).Length;

                    if (fileSize < 2097152)
                    {
                        newTrailImagesListItem = new TrailImage(fileName, filePath);
                        trailImageListItems.Add(newTrailImagesListItem);
                    }
                    else
                    {
                        MessageBox.Show(this, "The image " + fileName + " is too large (> 2MB) and cannot be added.", "Warning", MessageBoxButtons.OK);
                    }
                }

                if (newTrailImagesListItem != null)
                {
                    listBoxImages.SelectedItem = newTrailImagesListItem;
                    listBoxImages_SelectedIndexChanged(this, new EventArgs());
                }
            }
        }

        private void listBoxImages_SelectedValueChanged(object sender, EventArgs e)
        {
            
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                trailImageListItems.Remove(selectedItem);
            }

            listBoxImages_SelectedIndexChanged(this, new EventArgs());
        }

        private void textBoxGalleryCaption_TextChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                selectedItem.GalleryCaption = textBoxGalleryCaption.Text;
            }
        }

        private void textBoxImagePageText_TextChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
               selectedItem.PageText = textBoxImagePageText.Text;
            }
        }

        private void listBoxImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                // Populate the details area
                textBoxGalleryCaption.Text = selectedItem.GalleryCaption;
                textBoxImagePageText.Text = selectedItem.PageText;
                textBoxLocalPath.Text = selectedItem.LocalPath;
                textBoxDisplayText.Text = selectedItem.DisplayText;

                labelImageBox.Image = selectedItem.Image;

                groupBoxImageInfo.Enabled = true;
            }
            else
            {
                groupBoxImageInfo.Enabled = false;
            }
        }

        private void FormTrailImages_Shown(object sender, EventArgs e)
        {
            // Initialize image list...
            trailImageListItems = new  BindingList<TrailImage>();
            foreach (TrailImage trailImage in GlobalImageGalleryContainer.Instance.Images)
            {
                trailImageListItems.Add(trailImage);
            }

            // Bind image list...
            listBoxImages.DataSource = trailImageListItems;
            listBoxImages.DisplayMember = "DisplayText";
        }

        private void textBoxDisplayText_TextChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                selectedItem.DisplayText = textBoxDisplayText.Text;
            }
        }

        private void textBoxDisplayText_Validated(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                trailImageListItems.ResetItem(trailImageListItems.IndexOf(selectedItem));
            }
        }
    }
}
